import 'dart:math';

import 'package:flutter/material.dart';

class BuildingList extends StatefulWidget {
  const BuildingList({super.key, required this.point});

  final String point;

  @override
  State<BuildingList> createState() => _RoomListState();
}

class _RoomListState extends State<BuildingList> {
  Map data = {
    "point": "", // 记录小区信息
    "size": 0, // 随机产生楼栋的数量
    "type": "", // 小区的楼栋名称(size>4 单元, size<=4 号楼)
  };
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _mockData();
  }

  void _mockData() {
    Random r = Random();
    int size = r.nextInt(8) + 2; // 3- 8
    data["size"] = size;
    data["type"] = size > 5 ? "单元" : "号楼";
    data["point"] = widget.point;
    setState(() {}); // 响应式更新

    // PromptAction.showInfo(r.nextInt(8) + 2);
  }

  String getDisplayName(int index) {
    return data["point"] + "-" + index.toString() + data["type"];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('选择楼栋'),
      ),
      body: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return Container(
            color: Colors.white,
            padding: const EdgeInsets.all(10),
            child: GestureDetector(
              onTap: () {
                Navigator.pushNamed(context, "/room_list", arguments: {
                  "point": widget.point,
                  "build": (index + 1).toString() + data["type"]
                });
              },
              child: Row(
                children: [
                  Expanded(child: Text(getDisplayName(index + 1))),
                  const Row(
                    children: [
                      Icon(Icons.arrow_forward_ios,
                          size: 16, color: Colors.black),
                    ],
                  )
                ],
              ),
            ),
          );
        },
        itemCount: data["size"],
      ),
    );
  }
}
